package com.avic.rong.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.R;
import com.avic.rong.fragment.BootstrapOneView;
import com.avic.rong.fragment.BootstrapThreeView;
import com.avic.rong.fragment.BootstrapTwoView;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;

/**
 * Description:  开机引导页面
 * Author:       wangyz - 中国航空综合技术研究所
 * Email:        936804097@qq.com
 * Version:      1.0
 * Date:         2017-06-06
 * Copyright:    http://www.avic.com.cn Inc. All rights reserved.
 * 注意：本内容仅限于中国航空综合技术研究所内部传阅，禁止外泄以及用于其他的商业目
 **/
public class BootstrapActivity extends RxAppCompatActivity implements ViewPager.OnPageChangeListener {

    private List<View> viewList;
    private ViewPager viewPager;
    private CircleIndicator circleIndicator;
    private BootstrapOneView bootstrap_one_view;
    private BootstrapTwoView bootstrap_two_view;
    private BootstrapThreeView bootstrap_three_view;
    private Context mContext;
    private int SDK_PERMISSION_REQUEST = 300;
    private View statusBarView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bootstrap);

        this.mContext = this;
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewList = new ArrayList<View>();
        bootstrap_one_view = new BootstrapOneView(this);
        bootstrap_two_view = new BootstrapTwoView(this);
        bootstrap_three_view = new BootstrapThreeView(this);

        viewList.add(bootstrap_one_view);
        viewList.add(bootstrap_two_view);
        viewList.add(bootstrap_three_view);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(this);

        circleIndicator = (CircleIndicator) findViewById(R.id.indicator);
        circleIndicator.setViewPager(viewPager);

        overridePendingTransition(0, 0);
        SharedPreferencesUtil.getInstance().put(ConstantsUtils.START_UP_FIRST, true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            } else {
            }
        }

        //延时加载数据.
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                if (isStatusBar()) {
                    initStatusBar();
                    getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                            initStatusBar();
                        }
                    });
                }
                //只走一次
                return false;
            }
        });

    }

    private void initStatusBar() {
        if (statusBarView == null) {
            int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = getWindow().findViewById(identifier);
        }
        if (statusBarView != null) {
            statusBarView.setBackgroundResource(R.drawable.status_bar_title);
        }
    }

    protected boolean isStatusBar() {
        return true;
    }



    PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "title";
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    };


    public static Intent getIntent(Context context) {
        Intent intent = new Intent(context, BootstrapActivity.class);
        return intent;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.fade_out);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }



}
